<?php
namespace Admin\Controller;

class TermController extends BaseController {


    public function index() {
        $Term = D ('Term');
        $termList = $Term->getAllTerm();

        $this->assign('termList',$termList);
        $this->display();
    }

    /**
     * 学期添加页面
     */
    public function add() {
        if (!IS_POST) {
            $this->display();
        } else {

            $data['detail'] = $_REQUEST['detail'];
            $data['stime'] = $_REQUEST['stime'];
            $data['etime'] = $_REQUEST['etime'];
            $data['week'] = $_REQUEST['week'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->ajaxReturn(array('result'=>'RESULT_FAIL','msg'=>\Api\Libs\ExplainRespCodeUtil::explain($flag)));

            $Term = D ('Term');
            $flag = $Term->addTerm($data);
            if ($flag === '000000') {
                $this->ajaxReturn(array('result'=>'RESULT_OK','data'=>__APP__.'/Term'));
            }

            $this->ajaxReturn(array('result'=>'RESULT_FAIL','msg'=>\Api\Libs\ExplainRespCodeUtil::explain($flag)));
        }
    }

    /**
     * 修改学期
     */
    public function edit() {
        if (!IS_POST) {
            $id = $_REQUEST['id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Term = D ('Term');
            $t = $Term->getTermById($id);
            if (empty($t)) $this->error('学期不存在');

            $this->assign('term',$t);
            $this->display();
        } else {

            $data['id'] = $_REQUEST['id'];
            $data['detail'] = $_REQUEST['detail'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->ajaxReturn(array('result'=>'RESULT_FAIL','msg'=>\Api\Libs\ExplainRespCodeUtil::explain($flag)));

            $Term = D ('Term');
            if ($flag = $Term->editTerm($data) === '000000')
                $this->ajaxReturn(array('result'=>'RESULT_OK','data'=>__APP__.'/Term'));
            $this->ajaxReturn(array('result'=>'RESULT_FAIL','msg'=>\Api\Libs\ExplainRespCodeUtil::explain($flag)));
        }
    }


    /**
     * 删除学期方法
     */
    public function del() {
        $id = $_REQUEST['id'];
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->ajaxReturn(array('result'=>'RESULT_FAIL','msg'=>\Api\Libs\ExplainRespCodeUtil::explain($flag)));

        $Term = D ('Term');
        if (($flag = $Term->delTerm($id)) === '000000')
            $this->ajaxReturn(array('result'=>'RESULT_OK'));
        $this->ajaxReturn(array('result'=>'RESULT_FAIL','msg'=>\Api\Libs\ExplainRespCodeUtil::explain($flag)));
    }


    /**
     * 刷新学期状态
     */
    public function refreshState() {
        $Term = D ('Term');
        $Term->refreshState();
    }

} 